package com.example.sunmiv1.adapter;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import androidx.core.content.ContextCompat;

import com.example.sunmiv1.R;
import com.example.sunmiv1.activity.StringCodeOperationActivity;
import com.example.sunmiv1.entity.OnlineOrderBean;
import com.example.sunmiv1.entity.OrderDetailInfo;
import com.example.sunmiv1.entity.TurnoverBean;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class OnlineOrderPopupAdapter extends BaseAdapter {

    private Context context;
    private List<TurnoverBean.OrderBean.GoodsInfoBean> list;
    ViewHolder viewHolder;

    public OnlineOrderPopupAdapter(Context context, List<TurnoverBean.OrderBean.GoodsInfoBean> list) {
        this.context = context;
        this.list = list;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.online_order_popup_adapter, null);
            viewHolder = new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        viewHolder.onlineOrderPopupAdapterName.setText(list.get(position).getGoods_name());
        viewHolder.onlineOrderPopupAdapterNum.setText("" + list.get(position).getNum());
        viewHolder.onlineOrderPopupAdapterPrice.setText(list.get(position).getAll_money());
        if ("1".equals(list.get(position).getSerial_number_state())) {
            viewHolder.onlineOrderPopupAdapterNum.setTextColor(ContextCompat.getColor(context, R.color.blue_3293FA));
        } else {
            viewHolder.onlineOrderPopupAdapterNum.setTextColor(ContextCompat.getColor(context, R.color.text_666));
        }
        final int index = position;
        viewHolder.onlineOrderPopupAdapterNum.setOnClickListener(v -> {
            if ("1".equals(list.get(index).getSerial_number_state())) {
                choose.choose(index);
            }
        });
        return convertView;
    }

    private Choose choose;

    public void setChoose(Choose choose) {
        this.choose = choose;
    }

    public interface Choose {
        void choose(int index);
    }
    static class ViewHolder {
        @BindView(R.id.onlineOrderPopup_adapter_name)
        TextView onlineOrderPopupAdapterName;
        @BindView(R.id.onlineOrderPopup_adapter_num)
        TextView onlineOrderPopupAdapterNum;
        @BindView(R.id.onlineOrderPopup_adapter_price)
        TextView onlineOrderPopupAdapterPrice;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
